home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 237 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: new T[0] and sizeof(T)
  5. Date: 2 Feb 1996 15:49:05 GMT
  6. Organization: Computer Science, University of Melbourne, Australia
  7. Sender: news@cs.mu.OZ.AU (CS-Usenet)
  8. Approved: clamage@eng.sun.com (comp.std.c++)
  9. Message-ID: <9603318.21879@mulga.cs.mu.OZ.AU>
  10. References: <1996Feb1.091641.4676@iiasa.ac.at>
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Originator: clamage@taumet
  13.  
  14. marek@iiasa.ac.at (Marek  MAKOWSKI) writes:
  15.  
  16. >I would like to ask for comments on three easy questions illustrated 
  17. >by the following piece of code:
  18. >template <class I, class T>
  19. >void mVect<I,T>::resize(I new_size) {
  20. >   T *old = v;
  21. >   v = new T[new_size];    // T *v is a private member of mVect  
  22. >   int size_of_elem = sizeof(T);    // <--- question 3 
  23. >   // 
  24. >   // do something 
  25. >   // 
  26. >   delete[] old;     // <--- questions 1 & 2 
  27. >} 
  28. >I have the following questions:
  29. >1. Is it absolutely robust and portable to delete[] old, even
  30. >   if a previous call was for new_size == 0 (or if v was allocated
  31. >   by the ctor for the size == 0) ?
  32.  
  33. The code
  34.  
  35.     T *old = new T[0];
  36.     delete [] old;
  37.  
  38. is definitely perfectly legal (strictly conforming).
  39.  
  40. Whether or not it is in practice portable is another question -- that
  41. is something that can only be determined empirically.
  42.  
  43. >    In other words: is it guaranteed that:
  44. >   (v = new T[0]) == 0;
  45.  
  46. No, after 'v = new T[0]', it is guaranteed that `v' is *not* a null pointer.
  47.  
  48. >2. Is it correct to assume that no destructor is called by this statement
  49. >    if old was set as: old = new T[0]  ??
  50.  
  51. Yes.
  52.  
  53. >3. Is there any risk involved in using sizeof(T) in this statement ?
  54.  
  55. Basically no, but of course that would depend on what you used it for.
  56.  
  57. >The Watcom code blows-up on the delete[] statement (if a previous
  58. >call was for new_size == 0)
  59.  
  60. If what you have described is correct, then it sounds like a Watcom bug to me.
  61. (However, I suspect there is a good chance that there is a bug somewhere
  62. in the parts of your code that you haven't shown us.)
  63.  
  64. >and gives a warning whenever it sees the sizeof(T).
  65.  
  66. Compilers are allowed to warn about anything they like;
  67. however I don't see why warning about `sizeof(T)' would be useful.
  68.  
  69. >If the answer for quaestions 1 and 2 is negative then I would like to
  70. >know the reason why the standard does not require new to return 0
  71. >for zero_size array of objects.
  72.  
  73. Well, an array of size zero is conceptually different to no array at all.
  74.  
  75. >If there is a good reason for allowing new to return "anything" in such
  76. >situations then one should add to every ctor setting a ptr to 0
  77. >(which indeed made the Watcom version of my application running).
  78.  
  79. --
  80. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  81. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  82.  
  83. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  84.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  85.   summarized in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  86. ]
  87.